using System;
using System.Collections.Generic;
using System.Text;

namespace Genetic_Tetris
{
    public class Evaluator
    {
        PieceFactory pieceFac;
        MoveDecider decider;
        int rowsRemoved;
        public Evaluator()
        {
            pieceFac = new PieceFactory();
            decider = new MoveDecider();
            rowsRemoved = -1;
        }


        /// <summary>
        /// Plays the game with the specified heuristic,
        /// with the specified random seed,
        /// for the given number of pieces
        /// </summary>
        /// <param name="heuristic"></param>
        /// <param name="randSeed"></param>
        /// <param name="pieceCount">number of pieces to play the game for</param>
        /// <returns>Fitness of the run (rows removed)</returns>
        public int Evaluate(HeuristicConstants heuristic, int randSeed, int pieceCount)
        {

            int currPieceCount = 0;
            Board myBoard = new Board();
            myBoard.RowRemoved += new EventHandler(myBoard_RowRemoved);


            pieceFac.RandomSeed = randSeed;
            rowsRemoved = 0;


            Piece tempPiece;
            Move myMove = new Move();

            //DecideMove will return null if the game is lost
            while (myMove != null && currPieceCount < pieceCount)
            {
                tempPiece = pieceFac.MakePiece();
                myMove = decider.DecideMove(myBoard, tempPiece, heuristic);
                if (myMove != null)
                {
                    myBoard.PlacePieceInColumn(myMove.Piece, myMove.Column);
                    myBoard.ProcessCompleteRows();
                }
                currPieceCount++;
            }


            return rowsRemoved;
        }

        void myBoard_RowRemoved(object sender, EventArgs e)
        {
            rowsRemoved++;
        }


    }
}
